/**await-to-js  减少try catch块，便捷的捕获错误
 * @param promise 待检测的Promise
 * @returns 若Promise成功，返回[data, undefined]，若失败，返回[null, error] 
 * @example 
 * const pFn = async () => { //测试用的promise
 *    await sleep(1000)
 *    return 1
 *    // return Promise.reject('失败啦') //失败测试
 * }
 * const [data, err] = await to(pFn())
 * if (err) return console.log('失败', err);
 * console.log('成功拿到数据', data);
 */
const to = async <T, D = Error>(promise: Promise<T>): Promise<[T | null, D | undefined]> => {
    try {
        const res = await promise;
        return [res, undefined];
    } catch (err) {
        return [null, err as D];
    }
}
export default to